home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / 32SNIPIT.PAK / TBRSTRCT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.6 KB  |  281 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // tbrstrct.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[]       = "contacts";
  7. static const char szRstrctTblName[] = "rstrct";
  8. static const char szTblType[]       = szPARADOX;
  9.  
  10. #define NUMOFFIELDS 4
  11.  
  12. static DBIResult getFieldDescs(hDBICur hCur, UINT16 *uNumFields,
  13.                                FLDDesc **fldDesc);
  14. static DBIResult changeFieldDescs(UINT16 *uNumFields, FLDDesc *fldDesc,
  15.                                   FLDDesc *fldDescriptor, CROpType *ecrFldOp);
  16.  
  17. //=====================================================================
  18. //  Function:
  19. //          TBRestructure();
  20. //
  21. //  Description:
  22. //          This example shows how to change the characteristics of a
  23. //          table, adding, dropping and changing fields.
  24. //
  25. //          The following steps are followed:
  26. //              Getting the existing Field Descriptors
  27. //              Modifying the field descriptors to the new table structure
  28. //              Initializing the CRTblDesc (create table descriptor)
  29. //              Restructuring the table
  30. //=====================================================================
  31. void
  32. TBRestructure (void)
  33. {
  34.     hDBIDb      hDb;            // Handle to the database
  35.     hDBICur     hCur;           // Handle to the table
  36.     CRTblDesc   crTblDesc;      // Table descriptor
  37.     UINT16      uNumFields;     // Number of fields
  38.     UINT16      uNumOfRecs = 5; // No. of records to display
  39.     FLDDesc     *fldDesc;       // Pointer to the field descriptors
  40.     FLDDesc     fldDescriptor[NUMOFFIELDS]; // Field descriptor
  41.     CROpType    ecrFldOp[NUMOFFIELDS];      // Field operations
  42.     DBIResult   rslt;           // Return value from IDAPI functions
  43.     DBIPATH     szKeyViol = "KEYVIOL"; // Name for the key violation table
  44.  
  45.     Screen("*** Restructure Example ***\r\n");
  46.  
  47.     BREAK_IN_DEBUGGER();
  48.  
  49.     Screen("    Initializing IDAPI...");
  50.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  51.     {                                        
  52.         Screen("*** End of Example ***");
  53.         return;
  54.     }
  55.  
  56.     Screen("    Setting the database directory... ");
  57.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  58.     ChkRslt(rslt, "SetDirectory");
  59.  
  60.     Screen("    Open the %s table....", szTblName);
  61.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  62.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  63.                         xltFIELD, FALSE, NULL, &hCur);
  64.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  65.     {
  66.         CloseDbAndExit(&hDb);
  67.         Screen("\r\n*** End of Example ***");
  68.         return;
  69.     }
  70.  
  71.     rslt = DbiSetToBegin(hCur);
  72.     ChkRslt(rslt, "SetToBegin");
  73.  
  74.     Screen("    Display the first %u records of the %s table...",
  75.            uNumOfRecs, szTblName);
  76.     DisplayTable(hCur, uNumOfRecs);
  77.  
  78.     // Get the field descriptors for the existing table.
  79.     if (getFieldDescs(hCur, &uNumFields, &fldDesc) != DBIERR_NONE)
  80.     {
  81.         rslt = DbiCloseCursor(&hCur);
  82.         ChkRslt(rslt, "CloseCursor");
  83.         CloseDbAndExit(&hDb);
  84.         Screen("\r\n*** End of Example ***");
  85.         return;
  86.     }
  87.  
  88.     Screen("\r\n    Close the %s table...", szTblName);
  89.     DbiCloseCursor(&hCur);
  90.  
  91.     Screen("    Initialize the table descriptor...");
  92.     memset(&crTblDesc, 0, sizeof(CRTblDesc));
  93.     strcpy(crTblDesc.szTblName, szTblName);
  94.     strcpy(crTblDesc.szTblType, szTblType);
  95.     crTblDesc.bPack         = TRUE;
  96.     changeFieldDescs(&uNumFields, fldDesc, fldDescriptor, ecrFldOp);
  97.     crTblDesc.iFldCount     = uNumFields;
  98.     crTblDesc.pecrFldOp     = ecrFldOp;
  99.     crTblDesc.pfldDesc      = fldDescriptor;
  100.  
  101.     Screen("    Restructure the %s table to the %s table...",
  102.            szTblName, szRstrctTblName);
  103.     rslt = DbiDoRestructure(hDb, 1, &crTblDesc, (pCHAR) szRstrctTblName,
  104.                             szKeyViol, NULL, FALSE);
  105.     if (ChkRslt(rslt, "DoRestructure") != DBIERR_NONE)
  106.     {
  107.         free(fldDesc);
  108.         CloseDbAndExit(&hDb);
  109.         Screen("\r\n*** End of Example ***");
  110.         return;
  111.     }
  112.  
  113.     Screen("    Open the %s table...", szRstrctTblName);
  114.     rslt = DbiOpenTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType,
  115.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  116.                         xltFIELD, FALSE, NULL, &hCur);
  117.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  118.     {
  119.         free(fldDesc);
  120.         rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType);
  121.         ChkRslt(rslt, "DeleteTable");
  122.         CloseDbAndExit(&hDb);
  123.         Screen("\r\n*** End of Example ***");
  124.         return;
  125.     }
  126.  
  127.     rslt = DbiSetToBegin(hCur);
  128.     ChkRslt(rslt, "SetToBegin");
  129.  
  130.     Screen("    Display the first %u records of the %s table...",
  131.            uNumOfRecs, szRstrctTblName);
  132.     DisplayTable(hCur, uNumOfRecs);
  133.  
  134.     Screen("\r\n    Close the %s table...", szRstrctTblName);
  135.     rslt = DbiCloseCursor(&hCur);
  136.     ChkRslt(rslt, "CloseCursor");
  137.  
  138.     Screen("    Delete the %s table...", szRstrctTblName);
  139.     rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType);
  140.     ChkRslt(rslt, "DeleteTable");
  141.  
  142.     free(fldDesc);
  143.  
  144.     Screen("    Close the database and exit IDAPI...");
  145.     CloseDbAndExit(&hDb);
  146.  
  147.     Screen("\r\n*** End of Example ***");
  148. }
  149.  
  150. //===============================================================
  151. //  Function:
  152. //          getFieldDescs(hDBICur hCur, UINT16 *uNumFields, FLDDesc **fldDesc)
  153. //
  154. //  Input:  hCur            - Handle to the Cursor
  155. //          uNumFields      - Number of fields in the field descriptor.
  156. //                            (Returned)
  157. //          fldDesc         - Existing Field Descriptor (Input)
  158. //
  159. //  Return: IDAPI return code.
  160. //
  161. //  Description:
  162. //          This function gets the field descriptor for an existing cursor.
  163. //================================================================
  164. DBIResult getFieldDescs(hDBICur hCur, UINT16 *uNumFields, FLDDesc **fldDesc)
  165. {
  166.     DBIResult rslt;
  167.     CURProps curProps;
  168.  
  169.     // Change the translation mode to xltNONE to get the physical 
  170.     //   field descriptors.
  171.     rslt = DbiSetProp(hCur, curXLTMODE, xltNONE);
  172.     ChkRslt(rslt, "SetProp");
  173.  
  174.     rslt = DbiGetCursorProps(hCur, &curProps);
  175.     if (ChkRslt(rslt, "GetCursorProps") != DBIERR_NONE)
  176.     {
  177.         return rslt;
  178.     }
  179.  
  180.     *uNumFields = curProps.iFields;
  181.  
  182.     *fldDesc = (FLDDesc *) malloc(curProps.iFields * sizeof(FLDDesc));
  183.     if (fldDesc == NULL)
  184.     {
  185.         return DBIERR_NOMEMORY;
  186.     }
  187.  
  188.     rslt = DbiGetFieldDescs(hCur, *fldDesc);
  189.     if (ChkRslt(rslt, "GetFIeldDescs") != DBIERR_NONE)
  190.     {
  191.         return rslt;
  192.     }
  193.  
  194.     // Change the translation mode back to xltFIELD.
  195.     rslt = DbiSetProp(hCur, curXLTMODE, xltFIELD);
  196.     ChkRslt(rslt, "SetProp");
  197.  
  198.     return DBIERR_NONE;
  199. }
  200.  
  201. //===============================================================
  202. //  Function:
  203. //          changeFieldDescs(UINT16 *uNumFields, FLDDesc *fldDesc,
  204. //                           FLDDesc *fldDescriptor, CROpType *ecrFldOp);
  205. //
  206. //  Input:  uNumFields      - Number of fields in the existing field descriptor
  207. //                            Also returns the number of fields in the new
  208. //                            field descriptor. (Input and Returned)
  209. //          fldDesc         - Existing field descriptor (Input)
  210. //          fldDescriptor   - New field descriptor to use. (Returned)
  211. //          ecrFldOp        - Array of operations on the fields in
  212. //                            fldDescriptor. (Returned)
  213. //
  214. //  Return: IDAPI return code.
  215. //
  216. //  Description:
  217. //          This function sets up the field structure for the new table.
  218. //================================================================
  219. DBIResult changeFieldDescs(UINT16 *uNumFields, FLDDesc *fldDesc,
  220.                            FLDDesc *fldDescriptor, CROpType *ecrFldOp)
  221. {
  222.     UINT16      i;      // Loop Counter
  223.     UINT16      uField; // Number of the field
  224.  
  225.     uField = 0;
  226.     
  227.     for (i=0; i < *uNumFields; i++)
  228.     {
  229.         // Keep the "Last Name" field as is.
  230.         if (! strcmp(fldDesc[i].szName, "Last Name"))
  231.         {
  232.             fldDescriptor[uField] = fldDesc[i];
  233.             ecrFldOp[uField] = crNOOP;
  234.             uField++;
  235.         }
  236.  
  237.         // Change the "First Name" field.
  238.         if (! strcmp(fldDesc[i].szName, "First Name"))
  239.         {
  240.             fldDescriptor[uField] = fldDesc[i];
  241.             strcpy(fldDescriptor[uField].szName, "First");
  242.             ecrFldOp[uField] = crMODIFY;
  243.             uField++;
  244.         }
  245.  
  246.         // Move the "Phone" Field.
  247.         if (! strcmp(fldDesc[i].szName, "Phone"))
  248.         {
  249.             fldDescriptor[uField] = fldDesc[i];
  250.             ecrFldOp[uField] = crCOPY;
  251.             uField++;
  252.         }
  253.     }
  254.  
  255.     // Make certain that there is enough space in the Field Descriptor
  256.     //   for a new field.
  257.     if (uField < *uNumFields)
  258.     {
  259.         // Add the "Age" field.
  260.         fldDescriptor[uField].iFldNum        = 0;
  261.         strcpy(fldDescriptor[uField].szName, "Age");
  262.         fldDescriptor[uField].iFldType       = fldPDXSHORT;
  263.         fldDescriptor[uField].iSubType       = 0;
  264.         fldDescriptor[uField].iUnits1        = 0;
  265.         fldDescriptor[uField].iUnits2        = 0;
  266.         fldDescriptor[uField].iOffset        = 0;
  267.         fldDescriptor[uField].iLen           = 0;
  268.         fldDescriptor[uField].iNullOffset    = 0;
  269.         fldDescriptor[uField].efldvVchk      = fldvNOCHECKS;
  270.         fldDescriptor[uField].efldrRights    = fldrREADWRITE;
  271.  
  272.         ecrFldOp[uField] = crADD;
  273.  
  274.         uField++;
  275.     }
  276.     
  277.     *uNumFields = uField;
  278.  
  279.     return DBIERR_NONE;
  280. }
  281.